﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.TeamFoundation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Common;

namespace SourceControlAbstraction
{
    public class Server
    {
        static TfsTeamProjectCollection Collection;
        static WorkItemStore WorkItemStore;
        static VersionControlServer VersionControlServer;

        public static TfsTeamProjectCollection GetCollection()
        {
            if (Collection == null)
            {
                ConnectToServer();
            }
            return Collection;
        }

        public static WorkItemStore GetWorkItemStore()
        {
            if (Collection == null)
            {
                ConnectToServer();
            }
            return WorkItemStore;
        }

        public static VersionControlServer GetVersionControlServer()
        {
            if (Collection == null)
            {
                ConnectToServer();
            }
            return VersionControlServer;
        }

        protected static void ConnectToServer()
        {
            RegisteredProjectCollection[] collections = RegisteredTfsConnections.GetProjectCollections();

            
            // Yes this needs to be smarter!
            foreach (RegisteredProjectCollection collection in collections)
            {
                Collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collection);
                
                Collection.Connect(Microsoft.TeamFoundation.Framework.Common.ConnectOptions.IncludeServices);
                break;
            }

            VersionControlServer = Collection.GetService <VersionControlServer>();
            WorkItemStore = Collection.GetService<WorkItemStore>();
            
        }

    }
}
